##Chapter2Master.r
###master script for creating all Tables and Figures 
###in original analysis included in Chapter 4 of:
###Enos, Ryan D.  2017. The Space Between Us:
### Social Geography and Politics
####RdE November 2017

rm(list = ls())

##load packages
library(apsrtable)
library(ggplot2)
library(xtable)
library(RColorBrewer)
library(plyr)
library(Zelig)

##clustered standard errors function
cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  uj = uj[complete.cases(uj),]  

  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  return(vcovCL) }




source('TablesA14A15A16Figures4_3A1.r')
source('TablesA17A18A19A20.r')
source('TableA21.r')
source('TableA22.r')